package com.aydqyy.springbootstart.controller;

import com.aydqyy.springbootstart.entity.User;
import com.aydqyy.springbootstart.service.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/*@WebMvcTest(UserController.class) // 指定测试的Controller*/
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest2 {

    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private UserService userService;



    @BeforeEach
    void setUp() {
        // 如果需要手动初始化，可以在这里进行
    }

    @Test
    public void testRegister() throws Exception {
        Mockito.when(userService.getByName(anyString())).thenReturn(null);
        Mockito.when(userService.saveOrUpdate(any(User.class))).thenReturn(true);
        mockMvc.perform(post("/user/register")
                        .param("name", "test1")
                        .param("password", "123456"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0));
    }
}
